package net.haier.fashion.trend.report.core.common.model;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import net.haier.fashion.trend.report.core.common.enums.HttpReturnEnums;
import net.haier.fashion.trend.report.core.common.enums.IEnum;
import net.haier.fashion.trend.report.util.JsonUtil;

import java.io.Serializable;


/**
 * json结果
 *
 * @author zl
 * @date 2024/07/22
 */
@Getter
public class JsonResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;


    @ApiModelProperty(value = "状态码", notes = "200是成功,其他都是失败")
    private int code = HttpReturnEnums.SUCCESS.getValue();


    @ApiModelProperty(value = "状态码描述信息")
    private String msg = HttpReturnEnums.SUCCESS.getDesc();

    @ApiModelProperty(value = "具体的业务数据模型")
    private T data;



    public JsonResult() {
    }


    public JsonResult(T data) {
        this.data = data;
    }


    public JsonResult(IEnum e) {
        setCode(e.getValue());
        setMsg(e.getDesc());
    }


    public JsonResult(IEnum e, T data) {
        setCode(e.getValue());
        setMsg(e.getDesc());
        setData(data);
    }


    public JsonResult(IEnum e, T data, String message) {
        setCode(e.getValue());
        setMsg(message);
        setData(data);
    }


    public JsonResult<T> setCode(int code) {
        this.code = code;
        return this;
    }


    public JsonResult<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }


    public JsonResult<T> setData(T data) {
        this.data = data;
        return this;
    }


    public static <T> JsonResult<T> ok() {
        return JsonResult.get(HttpReturnEnums.SUCCESS);
    }


    public static <T> JsonResult<T> get(T data) {
        return new JsonResult<>(data);
    }


    public static <T> JsonResult<T> get(IEnum e) {
        return new JsonResult<>(e);
    }


    public static <T> JsonResult<T> get(IEnum e, T data) {
        return new JsonResult<>(e, data);
    }


    public static <T> JsonResult<T> get(IEnum e, T data, String message) {
        return new JsonResult<>(e, data, message);
    }

    @Override
    public String toString() {
        return JsonUtil.obj2string(this);
    }
}
